#include "AST.h"
#include <iostream>
using namespace std;

AST::AST(){}

	AST::AST(TNode node){
		root = node;
	}
	TNode AST::createNode(TYPE type, int index){
		TNode node(type,index);
		return node;
	}
	TNode AST::getRoot(){
		return root;
	}
	void AST::setRoot(TNode node){
		root = node;
	}
	void AST::setCurNode(TNode node){
		curNode = &node;
	}
	TNode* AST::getCurNode(){
		return curNode;
	}

	TNode AST::createNode(TYPE type, TNode parent) {
		TNode node(type, -1);
		node.setParent(parent);
		return node;
	}

	void AST::setChildren(TNode& node,TNode node2){
		node.setChild(node2);
	}


	vector<TNode> AST::getChildren(TNode node) {
		return node.getChild();
	}

	TNode AST::getParent(TNode node){
		TNode* parent = node.getParent();
		return *parent;
	}
	vector<TNode> AST::findPattern(TNode node){
		vector<TNode> toBeReturned, traversing,children;
		TNode curNode = root;
		//find pattern
		traversing.push_back(curNode);
		while(!traversing.empty()){
			curNode = traversing.at(0);
			if(curNode.isEqual(node))
				toBeReturned.push_back(curNode);
			if(!curNode.getChild().empty()){
				children = getChildren(curNode);
				traversing.insert(traversing.end(),children.begin(),children.end());
			}
			traversing.erase(traversing.begin());
			children.clear();
		}
		//found pattern
		return toBeReturned;
	}
	vector<TNode> AST::findPattern(TNode node, TNode startNode){
		vector<TNode> toBeReturned, traversing,children;
		TNode curNode;
		//find pattern
		traversing.push_back(startNode);
		while(!traversing.empty()){
			curNode = traversing.at(0);
			if(curNode.isEqual(node))
				toBeReturned.push_back(curNode);
			if(!curNode.getChild().empty()){
				children = getChildren(curNode);
				traversing.insert(traversing.end(),children.begin(),children.end());
			}
			traversing.erase(traversing.begin());
			children.clear();
		}
		//found pattern
		return toBeReturned;
	}